//
// Created by sgyt on 2020/3/17.
//  消息发送
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/msg.h>

#define MAX_TEXT 512

struct msg_st
{
    long int msg_type;
    char text[MAX_TEXT];
};

int main()
{
    struct msg_st data;
    char buffer[BUFSIZ];
    int res;
    int msgid = -1;
    //建立消息队列
    msgid = msgget((key_t)1234,0666 | IPC_CREAT);
    if(msgid < 0)
    {
        perror("msgget is error\n");
        exit(EXIT_FAILURE);
    }

    //向消息队列中写消息，直到写入end
    while(1)
    {
        //输入数据
        printf("Enter some text:");
        res =  read(STDIN_FILENO,buffer,BUFSIZ);
        data.msg_type = 1;
        strcpy(data.text,buffer);

        //向队列中发送数据
        if(msgsnd(msgid,(void *)&data,MAX_TEXT,0) == -1)
        {
            perror("msgsnd is error");
            exit(EXIT_FAILURE);
        }

        //输入end结束忽然
        if(strncmp(buffer,"end",strlen("end")) == 0)
        {
            break;
        }
    }
    return EXIT_SUCCESS;
}

